package com.zzxx.controller;

import com.github.pagehelper.PageInfo;
import com.zzxx.domain.Permission;
import com.zzxx.domain.Role;
import com.zzxx.domain.UserInfo;
import com.zzxx.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequestMapping("findAll")
    public String findAll(Model model,
                          @RequestParam(name = "pageNum", required = true, defaultValue = "1") Integer pageNum,
                          @RequestParam(name = "pageSize", required = true, defaultValue = "4") Integer pageSize){
        List<Role> list = roleService.queryAll(pageNum, pageSize);
        PageInfo<Role> rolePageInfo = new PageInfo<>(list);
        model.addAttribute("rolePageInfo", rolePageInfo);
        return "role-list";
    }

    @RequestMapping("/add")
    public String add(Role role){
        roleService.insertRole(role);
        // 页面跳转
        return "redirect:/role/findAll";
    }

    @RequestMapping("/findOtherPermissionsById")
    public String findOtherPermissionsById(@RequestParam(name = "id") String id, Model model){
        // 根据用户id找到用户没有的权限信息
        List<Permission> list = roleService.findOtherPermissionsById(id);
        model.addAttribute("list", list);
        model.addAttribute("roleId", id);
        return "role-add-permission";
    }

    @RequestMapping("/addPermission")
    public String addPermission(String[] ids,String roleId){
        System.out.println(Arrays.toString(ids));
        roleService.addPermission(ids, roleId);
        return "redirect:/role/findAll";
    }
}
